//
//  ZipRarManager.m
//  Compress
//
//  Created by 金色榜样研发组 on 2019/5/30.
//  Copyright © 2019 金色榜样研发组. All rights reserved.
//

#import "ZipRarManager.h"
#import <SSZipArchive-umbrella.h>
#import <UnrarKit.h>

@implementation ZipRarManager


+ (BOOL)unzipFileAtPath:(NSString *)filePath toDestination:(NSString *)destination withPassword:(nullable NSString *)password
{
    BOOL success;
    if ([self isEmpty:password]) {
        success = [SSZipArchive unzipFileAtPath:filePath toDestination:destination];
    }
    else
    {
        NSError *error = nil;
        success = [SSZipArchive unzipFileAtPath:filePath toDestination:destination overwrite:YES password:password error:&error];
        if (error) {
            NSLog(@"error = %@",error);
        }
    }
    return success;
}

+ (BOOL)unzipFileAtPath:(NSString *)filePath toDestinationn:(NSString *)destination withPassword:(nullable NSString *)password andProgressHandler:(void(^ _Nullable)(NSString *namePath, NSString *name, NSInteger currentIndex, NSInteger totalCount))handler completionHandler:(void (^)(NSString *path, BOOL succeeded, NSError * _Nullable error))completionHandler
{
    BOOL success = [SSZipArchive unzipFileAtPath:filePath toDestination:destination overwrite:YES password:password progressHandler:^(NSString * _Nonnull entry, unz_file_info zipInfo, long entryNumber, long total) {
        if (handler) {
            NSString *name = [entry lastPathComponent];
            handler(entry,name,entryNumber,total);
        }
    } completionHandler:^(NSString * _Nonnull path, BOOL succeeded, NSError * _Nullable error) {
        if (completionHandler) {
            completionHandler(path,succeeded,error);
        }
    }];
    return success;
}

#pragma mark -------------- 压缩单个或者多个文件[无回调](根据文件夹路径来压缩文件) --------------
+ (BOOL)createZipFileAtPath:(NSString *)filePath withContentsDirectory:(NSString *)directory withPassword:(nullable NSString *)password
{
    BOOL success;
    if ([self isEmpty:password]) {
        success = [SSZipArchive createZipFileAtPath:filePath withContentsOfDirectory:directory keepParentDirectory:YES];
    }
    else
    {
        success = [SSZipArchive createZipFileAtPath:filePath withContentsOfDirectory:directory keepParentDirectory:YES withPassword:password];
    }
    return success;
}

#pragma mark -------------- 压缩文件(根据文件夹路径来压缩文件) --------------
+ (BOOL)createZipFileAtPath:(NSString *)filePath withContentsDirectory:(NSString *)directory withPassword:(nullable NSString *)password andProgressHandler:(void(^ _Nullable)(NSInteger currentIndex, NSInteger totalCount))handler
{
    if ([self isEmpty:password]) {
        password = nil;
    }
    BOOL success = [SSZipArchive createZipFileAtPath:filePath withContentsOfDirectory:directory keepParentDirectory:YES withPassword:password andProgressHandler:^(NSUInteger entryNumber, NSUInteger total) {
        if (handler) {
            handler(entryNumber,total);
        }
        
    }];
    return success;
}
#pragma mark -------------- 压缩多个文件(根据多个文件的路径数组来压缩文件，此时无法监听压缩进度) --------------
+ (BOOL)createZipFileAtPath:(NSString *)filePath withFilesAtPaths:(NSArray *)paths withPassword:(nullable NSString *)password
{
    BOOL success;
    if ([self isEmpty:password]) {
        success = [SSZipArchive createZipFileAtPath:filePath withFilesAtPaths:paths];
    }
    else
    {
        success = [SSZipArchive createZipFileAtPath:filePath withFilesAtPaths:paths withPassword:password];
    }
    return success;
}

+ (BOOL)isEmpty:(NSString *)text
{
    if (!text || text.length == 0 || [[text lowercaseString] isEqualToString:@"null"] || [[text lowercaseString] isEqualToString:@"(null)"]) {
        return YES;
    }
    return NO;
}

@end
